var router = require("express").Router();

var User = require("../modules/db/user");
var md5 = require("md5");

router.post("/register", (req, res) => {
  User.findOne({ username: req.body.username }, (err, data) => {
    if (data) {
      res.json({
        error: 1,
        txt: "账号已存在",
      });
    } else {
      req.body.password = md5(req.body.password);
      req.body.password2 = md5(req.body.password2);
      var user = new User(req.body);
      user.save((err) => {
        res.json({ error: 0, txt: "注册成功" });
      });
    }
  });
});

module.exports = router;
